package com.bk.game.cat;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Disposable;
import com.bk.game.global.BkGlobal;

/**
 * 描述:猫类
 * 
 * @author linqiang(866116)
 * @Since 2012-9-5
 */
public class CatActor extends Actor implements Disposable {

	Cat cat;

	private Animation leftAnimation, rightAnimation;

	private TextureRegion currentFrame;// 当前帧

	private float stateTime;

	private boolean left;

	private boolean bang;

	private float scaleX, scaleY;

	private boolean down;

	private Object obj;

	private float start;

	public CatActor(int catIndex, int x, int y) {
		cat = BkGlobal.getInstance().getRandom();
		obj = new Object();
		this.x = x;
		this.y = y;
		leftAnimation = new Animation(0.5f, cat.catLeft);
		rightAnimation = new Animation(0.5f, cat.catRight);
		scaleX = 100 * BkGlobal.factorx;
		scaleY = 100 * BkGlobal.factory;
		// setDown(true);
	}

	@Override
	public void draw(SpriteBatch batch, float arg1) {
		if (bang) {
			if (!left) {
				batch.draw(cat.bangleft, x, y, scaleX, scaleY);
			} else {
				batch.draw(cat.bangright, x, y, scaleX, scaleY);
			}
		} else {
			stateTime += Gdx.graphics.getDeltaTime();
			if (left) {
				currentFrame = leftAnimation.getKeyFrame(stateTime, true);
			} else {
				currentFrame = rightAnimation.getKeyFrame(stateTime, true);
			}
			// if (isDown()) {
			// step = y;
			// } else {
			// step = x;
			// }
			// 得到下一帧
			batch.draw(currentFrame, x, y, scaleX, scaleY);
		}
	}

	@Override
	public Actor hit(float arg0, float arg1) {
		return null;
	}

	@Override
	public void dispose() {
	}

	public void change() {
		left = !left;
	}

	public boolean isLeft() {
		return left;
	}

	public void bang() {
		bang = true;
	}

	public String getName() {
		return cat.name;
	}

	public boolean isDown() {
		synchronized (obj) {
			return down;
		}
	}

	/**
	 * 方法描述:
	 * 
	 * @author lin.qiang
	 * @since 2012-9-8
	 * @param down
	 */
	public void setDown(boolean down) {
		synchronized (obj) {
			if (down) {
				start = y;
			} else {
				start = x;
			}
			this.down = down;
		}
	}

	public float getStart() {
		return start;
	}
}
